package com.rongc.client.freight.business.supply.view.activity;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.rongc.client.core.exception.RegexException;
import com.rongc.client.core.network.code.HandleCode;
import com.rongc.client.core.network.http.RequestManager;
import com.rongc.client.core.utils.ActivityUtils;
import com.rongc.client.core.utils.RegexUtil;
import com.rongc.client.freight.R;
import com.rongc.client.freight.base.BaseActivity;
import com.rongc.client.freight.base.request.result.NullResult;
import com.rongc.client.freight.business.mine.request.api.ComplainApi;
import com.rongc.client.freight.utils.UtilMethod;

import butterknife.Bind;

/**
 * Created by Bruce on 2017/7/20.
 */
public class ComplaintsActivity extends BaseActivity{
    @Bind(R.id.et_content)
    EditText mEtContent;

    String sourceId; // 货源车源id
    String fk_id;  // 被投诉人id

    Response.Listener<NullResult> respRespListener = new Response.Listener<NullResult>() {

        @Override
        public void onResponse(NullResult response) {
            if (HandleCode.requestSuccess()) {
                finish();
            } else {
                UtilMethod.dismissProgressDialog(ComplaintsActivity.this);
            }
        }
    };

    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            UtilMethod.dismissProgressDialog(ComplaintsActivity.this);
        }
    };

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.map_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_define:
                try{
                    prepare();
                    ComplainApi p = new ComplainApi(new ComplainApi.ComplainParams(
                            sourceId,fk_id,
                            mEtContent.getText().toString()),
                            respRespListener, errorListener);
                    RequestManager.getInstance().call(p);
                }catch (RegexException e){
                    ActivityUtils.toast(e.getMessage());
                }


                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    void prepare() throws RegexException {
        if(!RegexUtil.checkNotNull(mEtContent.getText().toString())){
            throw new RegexException("请输入投诉原因");
        }
    }

    @Override
    public void initParams() {
        sourceId = getIntent().getStringExtra("sourceId");
        fk_id = getIntent().getStringExtra("fk_id");
    }

    @Override
    public void initView() {
        initToolbar("投诉");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_complaints;
    }
}
